package org.example.components;

import com.almasb.fxgl.app.scene.GameView;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.time.TimerAction;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.util.Duration;
import org.example.sence.EndSence;
import org.example.sence.ShopSence;

public class GameTimerComponent extends Component {

    private Text timerText;
    private TimerAction timerAction;
    private int remainingSeconds;
    @Override
    public void onAdded() {
        // 创建计时器文本
        timerText = FXGL.getUIFactoryService().newText("", Color.DARKRED, 30);
        timerText.setLayoutX(FXGL.getAppWidth() - 70);
        timerText.setLayoutY(50);
        FXGL.getGameScene().addUINode(timerText);

        // 初始化倒计时（60秒）
        remainingSeconds = 1000;
        updateTimerDisplay();

        // 创建每秒更新的计时器
        timerAction = FXGL.getGameTimer().runAtInterval(() -> {
            remainingSeconds--;
            updateTimerDisplay();

            if (remainingSeconds <= 0) {
                timerAction.expire();

                // 时间到了 判断钱是否够了
                int money = FXGL.geti("money");
                if (money >= FXGL.geti("target")) {
//                    FXGL.getSceneService().pushSubScene(new EndSence());
                } else {
//                    FXGL.getSceneService().pushSubScene(new EndSence());
                }
            }
        }, Duration.seconds(1));
    }

    private void updateTimerDisplay() {
        timerText.setText(String.valueOf(remainingSeconds));
    }
}
